package us.wili.dev.netty;

import org.hibernate.validator.HibernateValidator;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import us.wili.dev.common.mybatis.IdAndTimeAutoUpdatePlugin;
import us.wili.dev.common.util.snowflake.SnowflakeUtil;


@SpringBootApplication(exclude = {
        WebMvcAutoConfiguration.class,
        SecurityAutoConfiguration.class,
        SessionAutoConfiguration.class,
        WebServicesAutoConfiguration.class},
        scanBasePackages = {"us.wili.dev"}
        )
@MapperScan(basePackages = {"us.wili.dev.dao"})
public class EndNettyBootApplication {

    public static void main(String[] args) throws Exception{
        SpringApplication netty = new SpringApplication(EndNettyBootApplication.class);
        netty.setWebEnvironment(false);
        ConfigurableApplicationContext context = netty.run(args);
        NettyTcpServer nettyTcpServer = context.getBean(NettyTcpServer.class);
    }

    /**
     * Mybatis插件，用于生成ID、版本号、创建时间、更新时间
     * @param idWorker
     * @return
     */
    @Bean
    public IdAndTimeAutoUpdatePlugin idAndTimeAutoUpdatePlugin(SnowflakeUtil idWorker , SnowflakeUtil versionWorkder) throws Exception {
        return new IdAndTimeAutoUpdatePlugin(idWorker, versionWorkder);
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        factoryBean.afterPropertiesSet();
        factoryBean.setProviderClass(HibernateValidator.class);

        return factoryBean;
    }

}
